<?php

Loader::using("Models::Entities::Puertos");

/**
 * Description of AdministrarPuertos
 *
 * @author JeyDotC
 * 
 * @export Modules
 * @export-metadata RequiredRoles:Agencia
 */
class AdministrarPuertos extends Module {

    private $puerto;
    private $puertosPerPage = 50;

    protected function onModuleReady() {
        $this->puerto = new Puertos();
    }

    public function index() {
        $paises = $this->components->Paises->getAll();
        $idPais = $this->request->get("idPais");
        $pagina = $this->request->get("page", 0);

        $puertos = array();

        if (!is_null($idPais)) {
            $puertos = $this->components->Puertos->getByPais($idPais, $pagina * $this->puertosPerPage, $this->puertosPerPage);
        } else {
            $this->addMessage(new Message("Por favor escoja un pais de la lista para ver los puertos.", "No hay pais seleccionado", Message::INFO));
        }

        $this->assign("puertos", $puertos);
        $this->assign("paises", $paises);
        $this->assign("idPais", $idPais);

        $this->display("index");
    }

    public function create() {
        $this->setupEditor("save");
        $this->display("create");
    }

    private function setupEditor($action, $backAction = "index",
            $backActionParams = array()) {
        $paises = $this->components->Paises->getAll();

        $this->assign("puerto", $this->puerto);
        $this->assign("action", $this->route->action($action));
        $this->assign("paises", $paises);
        $this->assign("back", $this->route->action($backAction, $backActionParams));
    }

    public function view() {
        $id = $this->request->get("id");

        /* @var $puerto Puertos */
        $puerto = $this->components->Puertos->getById($id);
        $terminales = $this->components->Terminales->getByPuerto($puerto->getIdPuerto());

        $this->assign("puerto", $puerto);
        $this->assign("terminales", $terminales);

        $this->display("view");
    }

}

?>
